<template>
  <div class="coupon-show">
    <couponOrderSelector :supportOverlay="!isCart" height="70%" :onlyShowCoupon="isCart" :info="couponData"
      :checkCodeParams="checkCodeParams" @submit="chooseCoupon"></couponOrderSelector>
  </div>
</template>

<script>
import Mimir from '@xiaoe/mimir';
import { MEMBER, SPECIAL_COLUMN, BIG_SPECIAL_COLUMN, TRAINING_CAMP, SVIP, ACTIVITY_MANAGE } from "../../utils/constants";

export default {
  components: {
    couponOrderSelector: () => Mimir({ section: 'market' }).get("coupon-order-selector"), // 「优惠券」优惠券/码展示
  },
  props: {
    isCart: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => ({})
    },
    goodsInfo: {
      type: Object,
      default: () => ({})
    },
    baseInfo: {
      type: Object,
      default: () => ({})
    },
    valuateData: {
      type: Object,
      default: () => ({})
    },
    marketingData: {
      type: Object,
      default: () => ({})
    },
  },
  data() {
    return {
    }
  },
  computed: {
    couponData() {
      const { coupon_info, price_info, promo_code_info } = this.valuateData || {};
      return {
        best_coupon: coupon_info?.recommended_coupons?.list[0],
        coupon_discount: promo_code_info?.promo_code ? promo_code_info?.promo_code_discount : price_info?.coupon_discount,
        coupon: {
          invalid: coupon_info?.invalid,
          valid: coupon_info?.valid,
          recommend: coupon_info?.recommended_coupons,
          choose_use_coupon: coupon_info?.selected_coupons,
          limit_svip: coupon_info?.limited_svip
        },
        promo_code: !(promo_code_info?.can_use_promo_code) ? '' : promo_code_info?.promo_code,  // 优惠码 - can_use_promo_code为 false 时，不可用
        optimal_coupon: coupon_info?.selected_coupons?.list[0],
        floor_type: price_info?.floor_type || 0,
        floor_price: price_info?.floor_price,
        original_price: price_info?.original_price,
        goods_price: price_info?.front_end_goods_price || 0,
      };
    },
    checkCodeParams() {
      const { promo_code_info } = this.valuateData || {};
      const spuType = this.goodsInfo.spu_type;
      const coursePackage = [MEMBER, SPECIAL_COLUMN, BIG_SPECIAL_COLUMN, TRAINING_CAMP];
      let paymentType = 2;
      if (coursePackage.includes(spuType)) {
        paymentType = 3;
      }
      if ([SVIP].includes(spuType)) {
        paymentType = 15
      }
      if ([ACTIVITY_MANAGE].includes(spuType)) {
        paymentType = 11
      }
      return {
        address_id: this.formData?.address?.id || '',
        count: this.baseInfo?.count || 1,
        resource_id: this.goodsInfo?.resource_id || '',
        spu_id: this.goodsInfo?.spu_id || '',
        sku_id: this.baseInfo.sku_id || '',
        resource_type: this.goodsInfo?.resource_type || '',
        spu_type: this.goodsInfo?.spu_type || '',
        product_id: '',
        payment_type: paymentType,
        promo_code: promo_code_info?.promo_code,
        activity_id: this.marketingData?.activity_id,
        activity_type: this.marketingData?.activity_type
      }
    }
  },
  methods: {
    //将传过来的优惠券列表数组  转换成 优惠券id 数组
    changeCouponId(data) {
      const arr = [];
      if (typeof data == 'object') {
        for (let i = 0; i < data.length; i++) {
          arr.push({
            cu_id: data[i]
          })
        }
      }
      return arr;
    },
    chooseCoupon({ choosedCoupon, usedPromoCode }) {
      console.log('choosedCoupon: ', choosedCoupon);
      let coupons = [];
      const cu_id = choosedCoupon.cu_id;
      if (cu_id !== 'couponNoUse') {
        const arr = Array.isArray(cu_id) ? cu_id : [cu_id];
        coupons = arr.map(id=>({cu_id: id}))
      }
      this.$emit("change", {
        coupons,
        promoCode: usedPromoCode
      })
    }
  },
}
</script>
<style>
.coupon-show {
  padding: 0 .32rem;
  background: #fff;
}
</style>
